<template>
	<div>
		  <p style="margin:10px auto;width:90%;">新闻添加</p>
		  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="width:80%;margin:20px auto;">
			  <el-form-item label="时间" required>
			    <el-col :span="11">
			      <el-form-item prop="date1">
			        <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1" style="width: 100%;"></el-date-picker>
			      </el-form-item>
			    </el-col>
			  </el-form-item>
			  <el-form-item label="名称" prop="name">
			    <el-input v-model="ruleForm.name"></el-input>
			  </el-form-item>
			  <el-form-item label="省份" prop="region">
			    <el-select v-model="ruleForm.region" placeholder="请选择省份">
			      <el-option label="湖北" value="湖北"></el-option>
			      <el-option label="广东" value="广东"></el-option>
			      <el-option label="四川" value="四川"></el-option>
			    </el-select>
			  </el-form-item>
			  <el-form-item label="城市" prop="city">
			    <el-input v-model="ruleForm.city"></el-input>
			  </el-form-item>
			  <el-form-item label="地址" prop="address">
			    <el-input v-model="ruleForm.address"></el-input>
			  </el-form-item>
			  <el-form-item label="邮编" prop="postcode">
			    <el-input v-model="ruleForm.postcode"></el-input>
			  </el-form-item>
			  <el-form-item>
			    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
			    <el-button @click="resetForm('ruleForm')">重置</el-button>
			  </el-form-item>
	    </el-form>
	</div>
</template>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          date1: '',
          name: '',
          region: '',
          city:'',
          address:'',
          postcode: ''
        },
        rules: {
          date1: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '请选择省份', trigger: 'change' }
          ],
          city: [
            { required: true, message: '请输入城市', trigger: 'blur' },
            { min: 2, max: 6, message: '长度在 2到 6个字符', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '请输入详细住址', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2到 20个字符', trigger: 'blur' }
          ],
          postcode: [
            { required: true, message: '请输入邮编', trigger: 'blur' },
            { min: 2, max: 7, message: '长度在 2到 7个字符', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios.post('http://localhost:3000/newslist',  { 
			          date:this.ruleForm.date1,
			          name:this.ruleForm.name,
			          province: this.ruleForm.region,
			          city: this.ruleForm.city,
			          address:this.ruleForm.address,
			          zip: this.ruleForm.postcode,
		    })   
		  .then(function (response) { console.log(response); })
		  .catch(function (error) { console.log(error); });
		  alert('新增成功');
		  
          } else {
            console.log('error submit!!');
            return false;
          }
          this.$router.replace('/news/newslist')
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>